Imports System
Imports NUnit.Framework
Imports ValidationFramework
Imports ValidationFramework.Configuration

Namespace Configuration
    Public Class CustomRuleConfigReaderExample
        Public Class Person
            ' Methods
            Private Shared Sub ValidateName(ByVal sender As Object, ByVal e As  _
                                               CustomValidationEventArgs)
                Dim name As String = CStr(e.TargetMemberValue)
                If String.IsNullOrEmpty(name) Then
                    e.IsValid = False
                Else
                    e.IsValid = True
                End If
            End Sub


            ' Properties
            Public Property Name() As String
                Get
                    Return _name
                End Get
                Set(ByVal value As String)
                    _name = value
                End Set
            End Property


            ' Fields
            Private _name As String
        End Class


        <TestFixture()> _
        Public Class ExampleUsage

            <Test()> _
            Public Sub RunCustomRuleConfigReader()
                ConfigurationService.AddXmlString(propertyXmlString)
                Dim person As New Person
                Dim propertyValidationManager = ValidationManagerFactory.GetPropertyValidationManager(person)
                propertyValidationManager.ValidateAll()
                Assert.IsFalse(propertyValidationManager.IsValid)
                person.Name = "John"
                propertyValidationManager.ValidateAll()
                Assert.IsTrue(propertyValidationManager.IsValid)
            End Sub


            ' Fields
            Private Shared propertyXmlString As String = "<?xml version='1.0' encoding='utf-8' ?>" & _
                                                         "<validationMapping xmlns='urn:validationFramework-validationDefinition-1.5'>" & _
                                                         "  <class typeName='ExampleLibraryVB.Configuration.CustomRuleConfigReaderExample+Person, ExampleLibraryVB'>" & _
                                                         "    <property name='Name'>" & _
                                                         "      <rule typeName='CustomRule' " & _
                                                         "            ruleInterpretation='This is a custom rule' " & _
                                                         "            validationMethod='ValidateName' " & _
                                                         "            validationTypeName='ExampleLibraryVB.Configuration.CustomRuleConfigReaderExample+Person, ExampleLibraryVB' " & _
                                                         "            errorMessage='hello' />" & _
                                                         "    </property>" & _
                                                         "  </class>" & _
                                                         "</validationMapping>"
        End Class
    End Class
End Namespace